package com.neu.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.neu.common.core.domain.entity.SysUser;
import com.neu.common.exception.UtilException;
import com.neu.common.utils.DateUtils;
import com.neu.common.utils.SecurityUtils;
import com.neu.system.domain.vo.NeuAirVO;
import com.neu.system.domain.vo.NeuTaskVo;
import com.neu.system.mapper.NeuAirMapper;
import com.neu.system.mapper.NeuCityMapper;
import com.neu.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neu.system.mapper.NeuTaskMapper;
import com.neu.system.domain.NeuTask;
import com.neu.system.service.INeuTaskService;

import static com.neu.common.utils.PageUtils.startPage;

/**
 * 【请填写功能名称】Service业务层处理
 *
 */
@Service
public class NeuTaskServiceImpl implements INeuTaskService
{
    @Autowired
    private NeuTaskMapper neuTaskMapper;

    @Autowired
    private NeuCityMapper neuCityMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private NeuAirServiceImpl airService;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public NeuTask selectNeuTaskById(Long id)
    {
        return neuTaskMapper.selectNeuTaskById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param neuTask 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<NeuTaskVo> selectNeuTaskList(NeuTask neuTask)
    {

        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = userMapper.selectUserById(userId);
        System.out.println(sysUser);
        Long deptId = sysUser.getDeptId();

        // 判断是否为aqi检测员
        if (deptId == 200) {
            neuTask.setCharge(userId);
        }

        // 结果列表
        List<NeuTaskVo> results = new ArrayList<>();
        // 数据处理
        startPage();
        List<NeuTask> neuTasks = neuTaskMapper.selectNeuTaskList(neuTask);

        // 数据封装返回
        for (NeuTask task : neuTasks) {
            NeuTaskVo vo = taskToTaskVo(task);

            results.add(vo);
        }

        return results;
    }

    /**
     * task转化为vo对象
     * @param task
     * @return
     */
    private NeuTaskVo taskToTaskVo(NeuTask task) {
        // 获取任务流程进度
        Long progress = task.getProgress();

        NeuTaskVo vo = new NeuTaskVo();

        Date createTime = task.getCreateTime();
        Date updateTime = task.getUpdateTime();

        vo.setCreateTime(createTime);
        vo.setNowTime(updateTime);
        vo.setProgressNum(progress);

        if (progress == 0) {
            vo.setProgress("用户反馈,尚未派分任务");
        } else if (progress == 1) {
            vo.setProgress("已派分任务,等待结果中");
        } else if (progress == 2) {
            vo.setProgress("收到测量数据，流程结束");
        }

        // 获取任务id
        vo.setId(task.getId());

        Long userId = task.getApplicant();
        vo.setApplicant(userMapper.selectUserById(userId).getNickName());
        vo.setMessage(task.getMessage());
        // 城市名称
        Long cityId = task.getCityId();
        String cityName = neuCityMapper.selectNeuCityByCityId(cityId);
        vo.setCityName(cityName);

        // 如果任务进程为0，没有负责人与测量结果信息，数据构建完成
        if (progress == 0) {
            return vo;
        }
        // 负责人信息
        Long chargeId = task.getCharge();
        SysUser sysUser = userMapper.selectUserById(chargeId);
        String phone = sysUser.getPhonenumber();
        vo.setChargePhone(phone);
        String nickName = sysUser.getNickName();
        vo.setChargeName(nickName);

        // 如果任务进程为1，测量结果信息，数据构建完成
        if (progress == 1) {
            return vo;
        }
        // 测量结果
        Long resultsId = task.getResults();
        NeuAirVO neuAirVO = airService.selectNeuAirVoById(resultsId);
        vo.setResults(neuAirVO);

        // 数据构建完成，放进去结果列表
        return vo;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param neuTask 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertNeuTask(NeuTask neuTask)
    {
        Long userId = SecurityUtils.getUserId();
        if (neuTask.getMessage() == null) {
            throw new UtilException("请填入关键信息");
        }

        if (neuTask.getCityId() == null) {
            throw new UtilException("请填入关键信息");
        }

        neuTask.setApplicant(userId);
        neuTask.setCharge(null);
        neuTask.setResults(null);
        neuTask.setProgress(0L);

        return neuTaskMapper.insertNeuTask(neuTask);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param neuTask 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateNeuTask(NeuTask neuTask)
    {
        neuTask.setCityId(null);
        neuTask.setApplicant(null);
        neuTask.setMessage(null);
        neuTask.setResults(null);
        neuTask.setProgress(1L);
        return neuTaskMapper.updateNeuTask(neuTask);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNeuTaskByIds(Long[] ids)
    {
        for (Long id : ids) {
            NeuTask task = neuTaskMapper.selectNeuTaskById(id);
            if (task.getProgress() == 0) {
                throw new UtilException("该任务尚未分派，禁止删除用户举报信息");
            }
        }

        return neuTaskMapper.deleteNeuTaskByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNeuTaskById(Long id)
    {
        NeuTask task = neuTaskMapper.selectNeuTaskById(id);
        if (task.getProgress() == 0) {
            throw new UtilException("该任务尚未分派，禁止删除用户举报信息");
        }
        return neuTaskMapper.deleteNeuTaskById(id);
    }

    /**
     *  反馈处理
     * @param neuTask
     * @return
     */
    @Override
    public int handleNeuTask(NeuTask neuTask) {
        neuTask.setCityId(null);
        neuTask.setApplicant(null);
        neuTask.setMessage(null);
        neuTask.setCharge(null);
        neuTask.setProgress(2L);
        return neuTaskMapper.updateNeuTask(neuTask);
    }

    /**
     * 获得未分配任务信息
     * @param neuTask
     * @return
     */
    @Override
    public List<NeuTaskVo> getNoSend(NeuTask neuTask) {

        neuTask.setProgress(0L);
        // 结果列表
        List<NeuTaskVo> results = new ArrayList<>();
        // 数据处理
        List<NeuTask> neuTasks = neuTaskMapper.selectNeuTaskList(neuTask);

        for (NeuTask task : neuTasks) {
            NeuTaskVo vo = new NeuTaskVo();

            vo.setId(task.getId());

            Long userId = task.getApplicant();
            vo.setApplicant(userMapper.selectUserById(userId).getNickName());
            vo.setMessage(task.getMessage());
            // 城市名称
            Long cityId = task.getCityId();
            String cityName = neuCityMapper.selectNeuCityByCityId(cityId);
            vo.setCityName(cityName);

            results.add(vo);
        }

        return results;
    }

    /**
     * 获得当前用户未处理举报信息
     * @param neuTask
     * @return
     */
    @Override
    public List<NeuTaskVo> getNoHandle(NeuTask neuTask) {
        neuTask.setProgress(1L);
        // 查询当前登录用户的待处理列表
        Long userId = SecurityUtils.getUserId();
        neuTask.setCharge(userId);
        // 结果列表
        List<NeuTaskVo> results = new ArrayList<>();
        // 数据处理
        List<NeuTask> neuTasks = neuTaskMapper.selectNeuTaskList(neuTask);

        for (NeuTask task : neuTasks) {
            NeuTaskVo vo = new NeuTaskVo();

            vo.setId(task.getId());

            vo.setMessage(task.getMessage());
            // 城市名称
            Long cityId = task.getCityId();
            String cityName = neuCityMapper.selectNeuCityByCityId(cityId);
            vo.setCityName(cityName);

            results.add(vo);
        }

        return results;
    }

    @Override
    public Long getHistoryCount(NeuTask neuTask) {
        Long count = neuTaskMapper.secectHistoryCount(neuTask);
        return count;
    }

    @Override
    public List<NeuTaskVo> getUserCommitHistory() {
        List<NeuTaskVo> result = new ArrayList<>();

        Long userId = SecurityUtils.getUserId();

        NeuTask task = new NeuTask();
        task.setApplicant(userId);
        List<NeuTask> neuTasks = neuTaskMapper.selectNeuTaskList(task);

        if (neuTasks.size() == 0) {
            throw new UtilException("没有查询到您的历史举报信息");
        }

        for (NeuTask task1 : neuTasks) {
            NeuTaskVo vo = taskToTaskVo(task1);

            result.add(vo);
        }

        return result;
    }

    @Override
    public NeuTaskVo getOneId(Long id) {
        NeuTask task = neuTaskMapper.selectNeuTaskById(id);
        NeuTaskVo vo = taskToTaskVo(task);
        return vo;
    }
}

